package com.anjiplus.template.gaea.business.modules.quartz.controller;


import com.anjiplus.template.gaea.business.modules.quartz.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

;

@Slf4j
@RestController
@RequestMapping(path = "/YyLogController")
public class YyLogController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value(value = "${spring.datasource.databaseType}")
    private String databaseType;


    /**
     * 修改
     *
     * @param param
     * @return
     */
    @PostMapping("/updateyygs")
    public Result updateyygs(@RequestBody Map<String, Object> param) {
        try {
            String updateSql = "UPDATE `yy_org_orgs` \n" +
                    "SET `NAME` = '" + param.get("NAME") + "',\n" +
                    "`yyCode` = '" + param.get("yyCode") + "',\n" +
                    "`yyztCode` = '" + param.get("yyztCode") + "',\n" +
                    "`gsCode` = '" + param.get("gsCode") + "',\n" +
                    "`gsztCode` = '" + param.get("gsztCode") + "',\n" +
                    "`kmtx` = '" + param.get("kmtx") + "',\n" +
                    "`yyorgPK` = '" + param.get("yyorgPK") + "',\n" +
                    "`yyztName` = '" + param.get("yyztName") + "',\n" +
                    "`yyztPK` = '" + param.get("yyztPK") + "',\n" +
                    "`ztlxPK` = '" + param.get("ztlxPK") + "',\n" +
                    "`ZTLXCODE` = '" + param.get("ZTLXCODE") + "',\n" +
                    "`ZTLXNAME` = '" + param.get("ZTLXNAME") + "'\n" +
                    "WHERE\n" +
                    "\t`id` = '" + param.get("id") + "';";
            int update = jdbcTemplate.update(updateSql);
            String msg = "";
            if (update > 0) {
                msg = "修改成功";
                return Result.success(msg);
            } else {
                msg = "修改失败";
                return Result.fail(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 修改
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteyygs")
    public Result deleteyygs(@RequestParam("id") Integer id) {
        try {
            String updateSql = "UPDATE `yy_org_orgs` \n" +
                    "SET  `delete_state` = '1'\n" +
                    "WHERE\n" +
                    "\t`id` = '" + id + "';";
            int update = jdbcTemplate.update(updateSql);
            String msg = "";
            if (update > 0) {
                msg = "删除成功";
                return Result.success(msg);
            } else {
                msg = "删除失败";
                return Result.fail(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询列表
     *
     * @param param
     * @return
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> param) {
        try {
            String condition = "";
            // 拼接条件
            if (!ObjectUtils.isEmpty(param.get("methodName"))) {
                condition += " and methodName = '" + param.get("methodName") + "'";
            }
            if (!ObjectUtils.isEmpty(param.get("state"))) {
                condition += " and state = '" + param.get("state") + "'";
            }
            if ("oracle".equals(databaseType)) {
                if (!ObjectUtils.isEmpty(param.get("ts"))) {
                    condition += " and ts >= to_date('" + param.get("ts") + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss')";
                    condition += " and ts <= to_date('" + param.get("ts") + " 23:59:59', 'yyyy-mm-dd hh24:mi:ss')";
                }
            } else {
                if (!ObjectUtils.isEmpty(param.get("ts"))) {
                    condition += " and ts = '" + param.get("ts") + " 00:00:00'";
                }
            }
            Integer count = jdbcTemplate.queryForObject("select count(*) from gs_yy_log where 1 = 1 " + condition, Integer.class);
            if ("oracle".equals(databaseType)) {
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from gs_yy_log where 1=1 " + condition + "order by TS desc OFFSET ((" + Integer.parseInt(param.get("pageNum").toString()) + " - 1 ) *" + Integer.parseInt(param.get("pageSize").toString()) + ") rows fetch next " + Integer.parseInt(param.get("pageSize").toString()) + " rows only");
                return Result.success(mapList, count);
            } else {
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from gs_yy_log where 1=1 " + condition + "limit " + Integer.parseInt(param.get("pageSize").toString()) + " OFFSET " + (Integer.parseInt(param.get("pageNum").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
                return Result.success(mapList, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询方法列表
     *
     * @param
     * @return
     */
    @PostMapping("/getMethodList")
    public Result getMethodList() {
        try {
            List<Map<String, Object>> mapList = jdbcTemplate.queryForList("SELECT DISTINCT methodName FROM gs_yy_log");
            return Result.success(mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


}
